/*     */ package com.zimbra.cs.service;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.LogFactory;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.auth.AuthContext.Protocol;
/*     */ import com.zimbra.cs.httpclient.URLUtil;
/*     */ import com.zimbra.cs.servlet.ZimbraServlet;
/*     */ import com.zimbra.cs.util.AccountUtil;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.io.StringReader;
/*     */ import java.io.StringWriter;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Enumeration;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import javax.xml.parsers.DocumentBuilder;
/*     */ import javax.xml.parsers.DocumentBuilderFactory;
/*     */ import javax.xml.transform.Transformer;
/*     */ import javax.xml.transform.TransformerFactory;
/*     */ import javax.xml.transform.dom.DOMSource;
/*     */ import javax.xml.transform.stream.StreamResult;
/*     */ import org.apache.commons.codec.binary.Base64;
/*     */ import org.w3c.dom.Document;
/*     */ import org.w3c.dom.Element;
/*     */ import org.w3c.dom.Node;
/*     */ import org.w3c.dom.NodeList;
/*     */ import org.xml.sax.InputSource;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class AutoDiscoverServlet
/*     */   extends ZimbraServlet
/*     */ {
/*     */   private static final long serialVersionUID = 1921058393177879727L;
/*  66 */   private static final Log log = LogFactory.getLog(AutoDiscoverServlet.class);
/*     */   
/*     */   private static final String BASIC_AUTH_HEADER = "Authorization";
/*     */   
/*     */   private static final String WWW_AUTHENTICATE_HEADER = "WWW-Authenticate";
/*     */   private static final String WWW_AUTHENTICATE_VALUE = "BASIC realm=\"Zimbra\"";
/*     */   private static final String NTLM = "NTLM";
/*     */   private static final String NEGOTIATE = "Negotiate";
/*     */   private static final String MS_ACTIVESYNC_PATH = "/Microsoft-Server-ActiveSync";
/*     */   private static final String EWS_SERVICE_PATH = "/ews/Exchange.asmx";
/*     */   private static final String NS_MOBILE = "http://schemas.microsoft.com/exchange/autodiscover/mobilesync/responseschema/2006";
/*     */   private static final String NS_OUTLOOK = "http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a";
/*     */   private static final String NS = "http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006";
/*     */   private static final String XSI_NS = "http://www.w3.org/2001/XMLSchema-instance";
/*     */   private static final String XSD_NS = "http://www.w3.org/2001/XMLSchema";
/*     */   
/*     */   public void init()
/*     */     throws ServletException
/*     */   {
/*  85 */     log.info("Starting up");
/*  86 */     super.init();
/*     */   }
/*     */   
/*     */   public void destroy()
/*     */   {
/*  91 */     log.info("Shutting down");
/*  92 */     super.destroy();
/*     */   }
/*     */   
/*     */   public static String getServiceUrl(Account acct, String client) throws ServiceException {
/*  96 */     Provisioning prov = Provisioning.getInstance();
/*  97 */     Server server = prov.getServer(acct);
/*  98 */     Domain domain = prov.getDomain(acct);
/*  99 */     String serviceUrl = "";
/*     */     
/* 101 */     if (!isEwsClient(client)) {
/* 102 */       if (LC.zimbra_activesync_autodiscover_use_service_url.booleanValue()) {
/* 103 */         serviceUrl = URLUtil.getServiceURL(server, "/Microsoft-Server-ActiveSync", true);
/*     */       } else {
/* 105 */         serviceUrl = URLUtil.getPublicURLForDomain(server, domain, "/Microsoft-Server-ActiveSync", true);
/*     */       }
/*     */       
/*     */     }
/* 109 */     else if (LC.zimbra_ews_autodiscover_use_service_url.booleanValue()) {
/* 110 */       serviceUrl = URLUtil.getServiceURL(server, "/ews/Exchange.asmx", true);
/*     */     } else {
/* 112 */       serviceUrl = URLUtil.getPublicURLForDomain(server, domain, "/ews/Exchange.asmx", true);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 117 */     if (serviceUrl.toLowerCase().startsWith("https")) {
/* 118 */       serviceUrl = serviceUrl.replace(":" + URLUtil.DEFAULT_HTTPS_PORT + "/", "/");
/* 119 */     } else if (serviceUrl.toLowerCase().startsWith("http")) {
/* 120 */       serviceUrl = serviceUrl.replace(":" + URLUtil.DEFAULT_HTTP_PORT + "/", "/");
/*     */     }
/*     */     
/* 123 */     return serviceUrl;
/*     */   }
/*     */   
/*     */   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
/*     */     throws ServletException, IOException
/*     */   {
/* 129 */     String userAgent = req.getHeader("User-Agent");
/* 130 */     if ((userAgent != null) && ((userAgent.contains("PocketPC")) || (userAgent.contains("SmartPhone")))) {
/* 131 */       if (log.isDebugEnabled()) {
/* 132 */         Enumeration<String> enm = req.getHeaderNames();
/* 133 */         while (enm.hasMoreElements()) {
/* 134 */           String header = (String)enm.nextElement();
/* 135 */           log.debug("GET header: %s", new Object[] { header + ":" + req.getHeader(header) });
/*     */         }
/*     */       }
/* 138 */       if (req.isSecure()) {
/* 139 */         Account acct = authenticate(req, resp, "http://schemas.microsoft.com/exchange/autodiscover/mobilesync/responseschema/2006");
/* 140 */         if (acct == null) {
/* 141 */           return;
/*     */         }
/*     */       } else {
/* 144 */         resp.sendRedirect(LC.zimbra_activesync_autodiscover_url.value());
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void doPost(HttpServletRequest req, HttpServletResponse resp)
/*     */     throws ServletException, IOException
/*     */   {
/* 169 */     ZimbraLog.clearContext();
/* 170 */     addRemoteIpToLoggingContext(req);
/*     */     
/* 172 */     log.info("Handling autodiscover request...");
/*     */     
/* 174 */     byte[] reqBytes = null;
/* 175 */     reqBytes = ByteUtil.getContent(req.getInputStream(), req.getContentLength());
/* 176 */     if (reqBytes == null) {
/* 177 */       log.warn("No content found in the request");
/* 178 */       sendError(resp, 600, "No content found in the request");
/* 179 */       return;
/*     */     }
/* 181 */     String content = new String(reqBytes, "UTF-8");
/* 182 */     log.debug("Request before auth: %s", new Object[] { content });
/*     */     
/* 184 */     if (log.isDebugEnabled()) {
/* 185 */       Enumeration<String> enm = req.getHeaderNames();
/* 186 */       while (enm.hasMoreElements()) {
/* 187 */         String header = (String)enm.nextElement();
/* 188 */         log.info("POST header: %s", new Object[] { header + ":" + req.getHeader(header) });
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 194 */     String email = null;
/* 195 */     String responseSchema = null;
/*     */     try
/*     */     {
/* 198 */       DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
/* 199 */       DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
/* 200 */       Document doc = docBuilder.parse(new InputSource(new StringReader(content)));
/* 201 */       NodeList nList = doc.getElementsByTagName("Request");
/*     */       
/* 203 */       for (int i = 0; i < nList.getLength(); i++) {
/* 204 */         Node node = nList.item(i);
/* 205 */         if (node.getNodeType() == 1) {
/* 206 */           Element element = (Element)node;
/* 207 */           email = getTagValue("EMailAddress", element);
/* 208 */           responseSchema = getTagValue("AcceptableResponseSchema", element);
/*     */           
/* 210 */           if (email != null)
/*     */             break;
/*     */         }
/*     */       }
/*     */     } catch (Exception e) {
/* 215 */       log.warn("cannot parse body: %s", new Object[] { content });
/* 216 */       sendError(resp, 400, "Body cannot be parsed");
/* 217 */       return;
/*     */     }
/*     */     
/*     */ 
/* 221 */     if ((email == null) || (email.length() == 0)) {
/* 222 */       log.warn("No Email address is specified in the Request, %s", new Object[] { content });
/* 223 */       sendError(resp, 400, "No Email address is specified in the Request");
/* 224 */       return;
/*     */     }
/*     */     
/*     */ 
/* 228 */     if ((responseSchema != null) && (responseSchema.length() > 0))
/*     */     {
/* 230 */       if ((!responseSchema.equals("http://schemas.microsoft.com/exchange/autodiscover/mobilesync/responseschema/2006")) && (!responseSchema.equals("http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a"))) {
/* 231 */         log.warn("Requested response schema not available " + responseSchema);
/* 232 */         sendError(resp, 503, "Requested response schema not available " + responseSchema);
/*     */         
/* 234 */         return;
/*     */       }
/*     */     }
/*     */     
/* 238 */     log.debug("Authenticating user");
/* 239 */     Account acct = authenticate(req, resp, responseSchema);
/* 240 */     if (acct == null) {
/* 241 */       return;
/*     */     }
/* 243 */     log.debug("Authentication finished successfully");
/*     */     
/* 245 */     log.debug("content length: %d, content type: %s", new Object[] { Integer.valueOf(req.getContentLength()), req.getContentType() });
/* 246 */     if ((req.getContentLength() == 0) || (req.getContentType() == null)) {
/* 247 */       log.warn("No suitable content found in the request");
/* 248 */       sendError(resp, 600, "No suitable content found in the request");
/* 249 */       return;
/*     */     }
/*     */     try
/*     */     {
/* 253 */       if (!AccountUtil.addressMatchesAccount(acct, email)) {
/* 254 */         log.warn(email + " doesn't match account addresses for user " + acct.getName());
/* 255 */         sendError(resp, 500, email + " doesn't match account addresses");
/*     */         
/* 257 */         return;
/*     */       }
/*     */     } catch (ServiceException e) {
/* 260 */       log.warn("Account access error; user=" + acct.getName(), e);
/* 261 */       sendError(resp, 500, "Account access error; user=" + acct.getName());
/*     */       
/* 263 */       return;
/*     */     }
/*     */     
/* 266 */     String respDoc = null;
/*     */     try {
/* 268 */       String serviceUrl = getServiceUrl(acct, responseSchema);
/* 269 */       String displayName = acct.getDisplayName() == null ? email : acct.getDisplayName();
/* 270 */       if (displayName.contains("@")) {
/* 271 */         displayName = displayName.substring(0, displayName.indexOf("@"));
/*     */       }
/* 273 */       log.debug("displayName: %s, email: %s, serviceUrl: %s", new Object[] { displayName, email, serviceUrl });
/* 274 */       if (isEwsClient(responseSchema)) {
/* 275 */         respDoc = createResponseDocForEws(displayName, email, serviceUrl, acct);
/*     */       } else {
/* 277 */         respDoc = createResponseDoc(displayName, email, serviceUrl);
/*     */       }
/*     */     } catch (Exception e) {
/* 280 */       log.warn(e);
/* 281 */       sendError(resp, 500, e.getMessage());
/* 282 */       return;
/*     */     }
/*     */     
/* 285 */     log.info("Response: %s", new Object[] { respDoc });
/* 286 */     log.debug("response length: %d", new Object[] { Integer.valueOf(respDoc.length()) });
/*     */     try
/*     */     {
/* 289 */       ByteUtil.copy(new ByteArrayInputStream(respDoc.getBytes("UTF-8")), true, resp.getOutputStream(), false);
/*     */     } catch (IOException e) {
/* 291 */       log.error("copy response error", e);
/* 292 */       sendError(resp, 500, e.getMessage());
/* 293 */       return;
/*     */     }
/*     */     
/* 296 */     log.debug("setting content type to text/xml");
/* 297 */     resp.setContentType("text/xml");
/* 298 */     log.info("sending autodiscover response...");
/*     */   }
/*     */   
/*     */ 
/*     */   private Account authenticate(HttpServletRequest req, HttpServletResponse resp, String responseSchema)
/*     */     throws ServletException, IOException
/*     */   {
/* 305 */     String auth = req.getHeader("Authorization");
/* 306 */     log.debug("auth header: %s", new Object[] { auth });
/*     */     
/* 308 */     if ((auth == null) || (!auth.toLowerCase().startsWith("basic "))) {
/* 309 */       log.warn("No basic auth header in the request");
/* 310 */       resp.addHeader("WWW-Authenticate", "BASIC realm=\"Zimbra\"");
/*     */       
/*     */ 
/* 313 */       sendError(resp, 401, "Unauthorized");
/* 314 */       return null;
/*     */     }
/*     */     
/*     */ 
/* 318 */     String cred = new String(Base64.decodeBase64(auth.substring(6).getBytes()));
/*     */     
/* 320 */     int bslash = cred.indexOf('\\');
/*     */     
/* 322 */     int colon = cred.indexOf(':');
/* 323 */     if ((colon == -1) || (colon <= bslash + 1)) {
/* 324 */       log.warn("Invalid basic auth credentials");
/* 325 */       sendError(resp, 400, "Invalid basic auth credentials");
/* 326 */       return null;
/*     */     }
/*     */     
/* 329 */     String domain = bslash > 0 ? cred.substring(0, bslash) : "";
/* 330 */     String userPassedIn = cred.substring(bslash + 1, colon);
/* 331 */     String user = cred.substring(bslash + 1, colon);
/* 332 */     String pass = cred.substring(colon + 1);
/*     */     
/* 334 */     log.debug("user=%s, domain=%s", new Object[] { user, domain });
/* 335 */     if (pass.length() == 0) {
/* 336 */       log.warn("Empty password");
/* 337 */       sendError(resp, 400, "Empty password");
/* 338 */       return null;
/*     */     }
/*     */     
/* 341 */     if ((domain.length() > 0) && (user.indexOf('@') == -1)) {
/* 342 */       if (domain.charAt(0) != '@') {
/* 343 */         user = user + '@';
/*     */       }
/* 345 */       user = user + domain;
/*     */     }
/*     */     try
/*     */     {
/* 349 */       Provisioning prov = Provisioning.getInstance();
/* 350 */       if (user.indexOf('@') == -1) {
/* 351 */         String defaultDomain = prov.getConfig().getAttr("zimbraDefaultDomainName", null);
/* 352 */         if (defaultDomain == null) {
/* 353 */           log.warn("Ldap access error; user=" + user);
/* 354 */           sendError(resp, 500, "ldap access error");
/* 355 */           return null;
/*     */         }
/* 357 */         user = user + "@" + defaultDomain;
/*     */       }
/*     */       
/* 360 */       Account account = prov.get(Key.AccountBy.name, user);
/* 361 */       if (account == null) {
/* 362 */         log.warn("User not found; user=" + user);
/* 363 */         resp.addHeader("WWW-Authenticate", "BASIC realm=\"Zimbra\"");
/* 364 */         sendError(resp, 401, "Invalid username or password");
/* 365 */         return null;
/*     */       }
/*     */       try
/*     */       {
/* 369 */         Map<String, Object> authCtxt = new HashMap();
/* 370 */         authCtxt.put("ocip", ZimbraServlet.getOrigIp(req));
/* 371 */         authCtxt.put("remoteip", ZimbraServlet.getClientIp(req));
/* 372 */         authCtxt.put("anp", userPassedIn);
/* 373 */         authCtxt.put("ua", req.getHeader("User-Agent"));
/* 374 */         prov.authAccount(account, pass, AuthContext.Protocol.zsync, authCtxt);
/*     */       } catch (ServiceException x) {
/* 376 */         log.warn("User password mismatch; user=" + user);
/* 377 */         resp.addHeader("WWW-Authenticate", "BASIC realm=\"Zimbra\"");
/* 378 */         sendError(resp, 401, "Invalid username or password");
/* 379 */         return null;
/*     */       }
/*     */       
/* 382 */       if ((isEwsClient(responseSchema)) && (!account.getBooleanAttr("zimbraFeatureEwsEnabled", false))) {
/* 383 */         log.info("User account not enabled for ZimbraEWS; user=" + user);
/* 384 */         sendError(resp, 403, "Account not enabled for ZimbraEWS");
/* 385 */         return null;
/*     */       }
/*     */       
/* 388 */       if ((!isEwsClient(responseSchema)) && (!account.getBooleanAttr("zimbraFeatureMobileSyncEnabled", false))) {
/* 389 */         log.info("User account not enabled for ZimbraSync; user=" + user);
/* 390 */         sendError(resp, 403, "Account not enabled for ZimbraSync");
/* 391 */         return null;
/*     */       }
/*     */       
/*     */ 
/* 395 */       return account;
/*     */     } catch (ServiceException x) {
/* 397 */       log.warn("Account access error; user=" + user, x);
/* 398 */       sendError(resp, 500, "Account access error; user=" + user); }
/* 399 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */   private static List<Element> getChildren(Element e)
/*     */   {
/* 405 */     NodeList nl = e.getChildNodes();
/* 406 */     List<Element> nodes = new ArrayList(nl.getLength());
/* 407 */     for (int i = 0; i < nl.getLength(); i++) {
/* 408 */       Node node = nl.item(i);
/* 409 */       if ((node instanceof Element)) {
/* 410 */         nodes.add((Element)node);
/*     */       }
/*     */     }
/* 413 */     return nodes;
/*     */   }
/*     */   
/*     */   private static String getTagValue(String tag, Element element) {
/* 417 */     NodeList nlList = element.getElementsByTagName(tag).item(0).getChildNodes();
/* 418 */     Node nValue = nlList.item(0);
/*     */     
/* 420 */     return nValue.getNodeValue();
/*     */   }
/*     */   
/*     */   private static void sendError(HttpServletResponse resp, int errCode, String reason) throws IOException {
/* 424 */     log.warn("HTTP/1.1 " + errCode + " " + reason);
/* 425 */     resp.sendError(errCode, reason);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static String createResponseDoc(String displayName, String email, String serviceUrl)
/*     */     throws Exception
/*     */   {
/* 467 */     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
/* 468 */     factory.setNamespaceAware(true);
/* 469 */     DocumentBuilder builder = factory.newDocumentBuilder();
/* 470 */     Document xmlDoc = builder.newDocument();
/*     */     
/* 472 */     Element root = xmlDoc.createElementNS("http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006", "Autodiscover");
/* 473 */     root.setAttribute("xmlns", "http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006");
/* 474 */     root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
/* 475 */     root.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
/* 476 */     xmlDoc.appendChild(root);
/*     */     
/*     */ 
/* 479 */     Element response = xmlDoc.createElementNS("http://schemas.microsoft.com/exchange/autodiscover/mobilesync/responseschema/2006", "Response");
/* 480 */     root.appendChild(response);
/*     */     
/*     */ 
/* 483 */     Element culture = xmlDoc.createElement("Culture");
/* 484 */     culture.appendChild(xmlDoc.createTextNode("en:en"));
/* 485 */     response.appendChild(culture);
/*     */     
/*     */ 
/* 488 */     Element user = xmlDoc.createElement("User");
/* 489 */     Element displayNameElm = xmlDoc.createElement("DisplayName");
/* 490 */     displayNameElm.appendChild(xmlDoc.createTextNode(displayName));
/* 491 */     user.appendChild(displayNameElm);
/* 492 */     Element emailAddr = xmlDoc.createElement("EMailAddress");
/* 493 */     emailAddr.appendChild(xmlDoc.createTextNode(email));
/* 494 */     user.appendChild(emailAddr);
/* 495 */     response.appendChild(user);
/*     */     
/*     */ 
/* 498 */     Element action = xmlDoc.createElement("Action");
/* 499 */     Element settings = xmlDoc.createElement("Settings");
/* 500 */     Element server = xmlDoc.createElement("Server");
/*     */     
/* 502 */     Element type = xmlDoc.createElement("Type");
/* 503 */     type.appendChild(xmlDoc.createTextNode("MobileSync"));
/* 504 */     server.appendChild(type);
/*     */     
/* 506 */     Element url = xmlDoc.createElement("Url");
/* 507 */     url.appendChild(xmlDoc.createTextNode(serviceUrl));
/* 508 */     server.appendChild(url);
/*     */     
/* 510 */     Element name = xmlDoc.createElement("Name");
/* 511 */     name.appendChild(xmlDoc.createTextNode(serviceUrl));
/* 512 */     server.appendChild(name);
/*     */     
/* 514 */     settings.appendChild(server);
/* 515 */     action.appendChild(settings);
/* 516 */     response.appendChild(action);
/*     */     
/* 518 */     TransformerFactory transformerFactory = TransformerFactory.newInstance();
/* 519 */     Transformer transformer = transformerFactory.newTransformer();
/* 520 */     transformer.setOutputProperty("omit-xml-declaration", "yes");
/* 521 */     transformer.setOutputProperty("indent", "yes");
/* 522 */     DOMSource source = new DOMSource(xmlDoc);
/* 523 */     StringWriter writer = new StringWriter();
/* 524 */     StreamResult result = new StreamResult(writer);
/* 525 */     transformer.transform(source, result);
/* 526 */     writer.flush();
/* 527 */     String xml = writer.toString();
/* 528 */     writer.close();
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 541 */     return "<?xml version=\"1.0\"?>\n" + xml;
/*     */   }
/*     */   
/*     */   private static String createResponseDocForEws(String displayName, String email, String serviceUrl, Account acct)
/*     */     throws Exception
/*     */   {
/* 547 */     Provisioning prov = Provisioning.getInstance();
/* 548 */     Server server = prov.getServer(acct);
/*     */     
/* 550 */     String cn = server.getCn();
/* 551 */     String name = server.getName();
/* 552 */     String acctId = acct.getId();
/*     */     
/*     */ 
/* 555 */     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
/* 556 */     factory.setNamespaceAware(true);
/* 557 */     DocumentBuilder builder = factory.newDocumentBuilder();
/* 558 */     Document xmlDoc = builder.newDocument();
/*     */     
/* 560 */     Element root = xmlDoc.createElementNS("http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006", "Autodiscover");
/* 561 */     root.setAttribute("xmlns", "http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006");
/* 562 */     root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
/* 563 */     root.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
/* 564 */     xmlDoc.appendChild(root);
/*     */     
/*     */ 
/* 567 */     Element response = xmlDoc.createElementNS("http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a", "Response");
/* 568 */     root.appendChild(response);
/*     */     
/*     */ 
/* 571 */     Element user = xmlDoc.createElement("User");
/* 572 */     Element displayNameElm = xmlDoc.createElement("DisplayName");
/* 573 */     displayNameElm.appendChild(xmlDoc.createTextNode(displayName));
/* 574 */     user.appendChild(displayNameElm);
/* 575 */     Element emailAddr = xmlDoc.createElement("EmailAddress");
/* 576 */     emailAddr.appendChild(xmlDoc.createTextNode(email));
/* 577 */     user.appendChild(emailAddr);
/*     */     
/* 579 */     Element depId = xmlDoc.createElement("DeploymentId");
/* 580 */     depId.appendChild(xmlDoc.createTextNode(acctId));
/* 581 */     user.appendChild(depId);
/*     */     
/*     */ 
/* 584 */     response.appendChild(user);
/*     */     
/*     */ 
/* 587 */     Element account = xmlDoc.createElement("Account");
/* 588 */     Element acctType = xmlDoc.createElement("AccountType");
/* 589 */     acctType.appendChild(xmlDoc.createTextNode("email"));
/* 590 */     account.appendChild(acctType);
/* 591 */     response.appendChild(account);
/*     */     
/*     */ 
/* 594 */     Element action = xmlDoc.createElement("Action");
/* 595 */     action.appendChild(xmlDoc.createTextNode("settings"));
/* 596 */     account.appendChild(action);
/*     */     
/*     */ 
/* 599 */     Element protocol = xmlDoc.createElement("Protocol");
/* 600 */     account.appendChild(protocol);
/*     */     
/* 602 */     Element type = xmlDoc.createElement("Type");
/* 603 */     type.appendChild(xmlDoc.createTextNode("EXCH"));
/* 604 */     protocol.appendChild(type);
/*     */     
/* 606 */     Element ews = xmlDoc.createElement("EwsUrl");
/* 607 */     protocol.appendChild(ews);
/* 608 */     ews.appendChild(xmlDoc.createTextNode(serviceUrl));
/*     */     
/* 610 */     TransformerFactory transformerFactory = TransformerFactory.newInstance();
/* 611 */     Transformer transformer = transformerFactory.newTransformer();
/* 612 */     transformer.setOutputProperty("omit-xml-declaration", "yes");
/* 613 */     transformer.setOutputProperty("indent", "yes");
/* 614 */     DOMSource source = new DOMSource(xmlDoc);
/* 615 */     StringWriter writer = new StringWriter();
/* 616 */     StreamResult result = new StreamResult(writer);
/* 617 */     transformer.transform(source, result);
/* 618 */     writer.flush();
/* 619 */     String xml = writer.toString();
/* 620 */     writer.close();
/* 621 */     return "<?xml version=\"1.0\"?>\n" + xml;
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception {
/* 625 */     System.out.println(createResponseDoc("test user", "b@c.com", "http://mail.com/MS-ActiveSync"));
/*     */   }
/*     */   
/*     */   public static boolean isEwsClient(String responseSchema) {
/* 629 */     boolean ewsClient = false;
/* 630 */     if (responseSchema.equals("http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a")) {
/* 631 */       ewsClient = true;
/*     */     }
/*     */     
/* 634 */     return ewsClient;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/AutoDiscoverServlet.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */